﻿using Docker.DotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet.Models;
using DockerPlay;
using Xunit;
using Xunit.Sdk;

namespace Tests
{
    public class TestStartInstances
    {
        private readonly DockerClient client;


        public TestStartInstances()
        {
         
            client = new DockerClientConfiguration(new Uri(ConnectionStrings.DockerMachineGA)).CreateClient();
        }

      

        [Fact]
        public async Task StartContainersAsync()
        {

            var containers = await client.Containers.ListContainersAsync(new ContainersListParameters() { Limit = 10 });

            foreach (var cont in containers)
            {
                await client.Containers.StartContainerAsync(cont.ID, new ContainerStartParameters());

                var status = await GetContainerStatus(cont.ID);
                Assert.True(status == "running");
            }
        }

        [Fact]
        public async Task StopContainersAsync()
        {
            var containers = await client.Containers.ListContainersAsync(new ContainersListParameters() { Limit = 10 });

            foreach (var cont in containers)
            {
                await
                    client.Containers.StopContainerAsync(cont.ID, new ContainerStopParameters(), CancellationToken.None);
            }
        }

        private async Task<string> GetContainerStatus(string containerId)
        {
            var inspect = await client.Containers.InspectContainerAsync(containerId);
            return inspect.State.Status;
        }

        [Fact]
        public async Task InspectContainer()
        {
            var containers = await client.Containers.ListContainersAsync(new ContainersListParameters() { Limit = 10 });

            foreach (var cont in containers)
            {
                var inspect = await client.Containers.InspectContainerAsync(cont.ID);
                Assert.NotNull(inspect);
            }
        }
    }
}
